package com.mytest.provider.service.config.security.userdetails;


import com.myrest.provider.poi.work.entities.user.SysUser;
import com.mytest.provider.service.config.security.SecurityUserDetails;
import com.mytest.provider.service.exception.LoginFailLimitException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;


/**
 * 系统用户登录
 */
@Slf4j
@Component
public class AdminUserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /*@Autowired
    private ISysUserService iSysUserService;*/



    /**
     * 系统用户登录
     *
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
//    @LogAnnotation(operateType = 1, operationName = "用户登录")
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

//        String flagKey = "loginFailFlag:" + username;
//        String value = redisTemplate.opsForValue().get(flagKey);
//        Long timeRest = redisTemplate.getExpire(flagKey, TimeUnit.MINUTES);
//        if (StringUtils.isNotBlank(value)) {
//            //超过限制次数
//            throw new LoginFailLimitException("登录错误次数超过限制，请" + timeRest + "分钟后再试");
//        }
//        SysUser sysUser = iSysUserService.selectByUserName(username);
        return new SecurityUserDetails(new SysUser());
    }
}
